/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.viewpagerindicator;

import com.zhpan.indicator.base.BaseIndicatorComponent;
import com.zhpan.indicator.utils.Log;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import java.io.IOException;

/**
 * 文字类型
 *
 * @since 2021-04-13
 */
public class FigureIndicatorComponent extends BaseIndicatorComponent implements Component.DrawTask {
    private int radius = AttrHelper.vp2px(20, getContext());
    private Color backgroundColor = new Color(Color.getIntColor("#88FF5252"));
    private Color textColor = new Color(Color.getIntColor("#FFFFFF"));
    private int textSize = AttrHelper.vp2px(14, getContext());
    private Paint mPaint;

    /**
     * 创建文字类型的指示器
     *
     * @param context 上下文
     */
    public FigureIndicatorComponent(Context context) {
        this(context, null);
    }

    /**
     * 创建文字类型的指示器
     *
     * @param context 上下文
     * @param attrs 参数
     */
    public FigureIndicatorComponent(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * 创建文字类型的指示器
     *
     * @param context 上下文
     * @param attrs 参数
     * @param defStyleAttr 样式
     */
    public FigureIndicatorComponent(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        init();
    }

    private void init() {
        addDrawTask(this);
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    /**
     * 设置文字大小
     *
     * @param size 文字大小
     * @return 指示器
     */
    public FigureIndicatorComponent setTextSize(int size) {
        this.textSize = size;
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param color 文字颜色
     * @return 指示器
     */
    public FigureIndicatorComponent setTextColor(Color color) {
        this.textColor = color;
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param resColor 文字颜色
     * @return 指示器
     */
    public FigureIndicatorComponent setTextColor(int resColor) {
        try {
            this.textColor = new Color(getResourceManager().getResource(resColor).available());
        } catch (IOException | NotExistException e) {
            Log.e("Fig", e.getMessage());
        }
        return this;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (getPageSize() > 1) {
            mPaint.setColor(backgroundColor);
            canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, mPaint);
            mPaint.setColor(textColor);
            mPaint.setTextSize(textSize);
            String text = getCurrentPosition() + 1 + "/" + getPageSize();
            int textWidth = (int) mPaint.measureText(text);
            Paint.FontMetrics fontMetricsInt = mPaint.getFontMetrics();
            float baseline =
                (getEstimatedHeight() - fontMetricsInt.bottom + fontMetricsInt.top) / 2 - fontMetricsInt.top;
            canvas.drawText(mPaint, text, (getWidth() - textWidth) / 2f, baseline);
        }
    }
}
